$(function() {
    $.ajax({
        url: ApiUrl + "/user/eb/getEbTypeEm",
        type: 'post',
        dataType: 'json',
        success: function(result) {
            $.each(result, function(k, v) {
                $.ajax({
                    url: ApiUrl + "/user/eb/getCommodityPage",
                    type: 'post',
                    data:{ebTypeEm:v},
                    dataType: 'json',
                    success: function(result) {
                        if (result.total>0) {
                            result.title = k;
                            result.catkey = v; 
                            var html = '';
                            html += template.render("goods", result);
                            if (result.catkey=='ZTQ') {
                                $('#cat-ztq').html(html);
                            }
                            if (result.catkey=='ZTQDC') {
                                $('#cat-ztqdc').html(html);
                            }
                            if (result.catkey=='TLJ') {
                                $('#cat-tlj').html(html);
                            }
                            if (result.catkey=='DTEJ') {
                                $('#cat-dtej').html(html);
                            }
                        };
                    }
                });
            });
        }
    });

    /*热门推荐商品*/
    $.ajax({
        url: ApiUrl + "/user/eb/getCommodityPage",
        type: 'post',
        data:{recommend:1},
        dataType: 'json',
        success: function(result) {
            console.log(result)
            if (result.total>0) {
                result.title = '推荐商品';
                result.recommend = 1; 
                var html = '';
                html += template.render("goods", result);
                $('#cat-recomend').html(html);
            };
            
        }
    });
    //slider广告
    $.ajax({
        url: ApiUrl + "/user/eb/getCommodityPage",
        type: 'post',
        data:{recommend:1},
        dataType: 'json',
        success: function(result) {
            var html = '';
            html += template.render("adv_list", result);
            $(".adv").html(html);
            $('.adv_list').each(function() {
            if ($(this).find('.item').length < 2) {
                return;
            }

            Swipe(this, {
                startSlide: 2,
                speed: 400,
                auto: 3000,
                continuous: true,
                disableScroll: false,
                stopPropagation: false,
                callback: function(index, elem) {},
                transitionEnd: function(index, elem) {}
            });
        });
        }
    });
    
    $('.search-btn').click(function(){
        var keyword = encodeURIComponent($('#keyword').val());
        location.href = WapSiteUrl+'/tmpl/product_list.html?keyword='+keyword;
    });

    // 微信分享
    var imgUrl = 'http://douting.com.cn/tlyht/user/upload/927f88ec-623e-409f-bc8c-e2747b1850ff.jpg'; 
    var lineLink = location.href; 
    var descContent = "听力宝商城听力检测仪T100隆重上市！优惠多多！"; 
    var shareTitle = '听力宝商城'; 
    var appid = 'wx26397a85f44acb94'; 
     
    function shareFriend() {
        WeixinJSBridge.invoke('sendAppMessage',{ 
        "appid": appid, 
        "img_url": imgUrl, 
        "img_width": "350", 
        "img_height": "350", 
        "link": lineLink, 
        "desc": descContent, 
        "title": shareTitle
        },function(res) { 
         // console.log('send_msg', res); 
        }) 
    } 
    function shareTimeline() {
        WeixinJSBridge.invoke('shareTimeline',{
        "img_url": imgUrl, 
        "img_width": "350", 
        "img_height": "350", 
        "link": lineLink, 
        "desc": descContent, 
        "title": shareTitle 
        }, function(res) {
            // console.log('timeline', res); 
        }); 
    } 
    function shareWeibo() {
        WeixinJSBridge.invoke('shareWeibo',{ 
        "content": descContent, 
        "url": lineLink, 
        }, function(res) { 
            // console.log('weibo', res); 
        }); 
    } 
    function shareQQ() {
        WeixinJSBridge.invoke('shareQQ',{ 
        "img_url": imgUrl, 
        "img_width": "350", 
        "img_height": "350", 
        "link": lineLink, 
        "desc": descContent, 
        "title": shareTitle 
        }, function(res) { 
            // console.log('shareQQ', res); 
        }); 
    } 
    function shareQZone() {
        WeixinJSBridge.invoke('shareQZone',{ 
        "img_url": imgUrl, 
        "img_width": "350", 
        "img_height": "350", 
        "link": lineLink, 
        "desc": descContent, 
        "title": shareTitle
        }, function(res) {
            // console.log('shareQZone', res); 
        });
    } 
    // 当微信内置浏览器完成内部初始化后会触发WeixinJSBridgeReady事件。 
    document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() { 
        // 发送给好友 
        WeixinJSBridge.on('menu:share:appmessage', function(argv){
            shareFriend();
        }); 
        // 分享到朋友圈 
        WeixinJSBridge.on('menu:share:timeline', function(argv){ 
            shareTimeline();
        }); 
        // 分享到微博 
        WeixinJSBridge.on('menu:share:weibo', function(argv){ 
            shareWeibo();
        });
        // 分享到QQ
        WeixinJSBridge.on('menu:share:qq', function(argv){ 
            shareQQ();
        });
        // 分享到QQ空间
        WeixinJSBridge.on('menu:share:qzone', function(argv){ 
            shareQZone();
        });
    }, false);
});
